﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Web.Codes;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class PhilHealthChartTable : BasePage
    {
        [Inject]
        public IPhilHealthTableService _philHealthService { get; set; }

        public bool IsSuccess { get; set; }
        public string  NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _philHealthService.GetAll();
            repeater.DataBind();
        }

        void Delete(int id)
        {
            _philHealthService.Delete(id);
            IsSuccess = _philHealthService.Save();
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }

        void Save(int id, RepeaterItem item)
        {
            var phTable = _philHealthService.GetById(id);

            var startRangeTxt = ((TextBox)item.FindControl("CodeLowTxt")).Text;
            var endRangeTxt = ((TextBox)item.FindControl("CodeHighTxt")).Text;
            var baseSalaryTxt = ((TextBox)item.FindControl("SalaryBaseTxt")).Text;

            double startRange;
            double endRange;
            double baseSalary;

            double.TryParse(startRangeTxt, out startRange);
            double.TryParse(endRangeTxt, out endRange);
            double.TryParse(baseSalaryTxt, out baseSalary);

            phTable.StartRange = startRange;
            phTable.EndRange = endRange;
            phTable.Base = baseSalary;

            _philHealthService.Update(phTable);
            IsSuccess = _philHealthService.Save();
            NotifyText = "Successfully Updated.";
            BindGrid();
        }

        void Add()
        {
            var phTable = new PhilHealthTable();
            
            double startRange;
            double endRange;
            double baseSalary;

            double.TryParse(this.MSBLowTxt.Text, out startRange);
            double.TryParse(this.MSBHighTxt.Text, out endRange);
            double.TryParse(this.BaseSalaryTxt.Text, out baseSalary);

            phTable.StartRange = startRange;
            phTable.EndRange = endRange;
            phTable.Base = baseSalary;

            _philHealthService.Add(phTable);
            IsSuccess = _philHealthService.Save();
            NotifyText = "Successfully Added New Record.";
            BindGrid();            
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindGrid();
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Save_Command:
                        Save(intTemp, e.Item);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void AddNewBtn_Click(object sender, EventArgs e)
        {
            Add();
            if (IsSuccess)
            {
                this.MSBLowTxt.Text = string.Empty;
                this.MSBHighTxt.Text = string.Empty;
                this.BaseSalaryTxt.Text = string.Empty;
            }
        }
    }
}